package m3;

import com.rabbitmq.client.BuiltinExchangeType;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

import java.util.Scanner;
import java.util.zip.DeflaterOutputStream;

/*
    2022/1/20
    15:30
*/public class Producer {
    public static void main(String[] args) throws Exception{
        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost("192.168.64.140");
        factory.setPort(5672);
        factory.setUsername("admin");
        factory.setPassword("admin");
        Connection connection = factory.newConnection();
        Channel channel = connection.createChannel();

        //创建交换机 fanout
        //channel.exchangeDeclare("logs", BuiltinExchangeType.FANOUT,true); //默认非持久
        channel.exchangeDeclare("logs", BuiltinExchangeType.FANOUT);

        //向交换机发送消息
        while (true) {
            System.out.println("请输入消息:");
            String s = new Scanner(System.in).nextLine();
            //对 fanout 交换机，指定 路由键(队列名) 无效
            channel.basicPublish("logs","",null,s.getBytes() );
            System.out.println("发送成功~");
        }
    }
}
